package com.xiaomi.baidumap.favorite.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.j256.ormlite.misc.IOUtils;
import com.xiaomi.baidumap.favorite.model.LocalRepo;
import com.xiaomi.baidumap.favorite.model.RepoGroup;

import org.apache.commons.io.IOUtil;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 16-9-1.
 */
public class LocalPresenter {

    //解析本地的数据json
    private static List<RepoGroup> repoGroups;
    public static List<RepoGroup> repoPresenter(Context context){
        repoGroups = new ArrayList<>();
        try {
            InputStream inputStream = context.getAssets().open("repogroup.json");
            String repoData = IOUtil.toString(inputStream);
            Gson gson = new Gson();
            repoGroups = gson.fromJson(repoData, new TypeToken<List<RepoGroup>>() {
            }.getType());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return repoGroups;
    }

    //解析LocalRepo数据
    private static List<LocalRepo> localRepos;
    public static List<LocalRepo> getLocalRepo(Context context){
        if (localRepos != null){
            return localRepos;
        }
        try {
            InputStream inputStream = context.getAssets().open("defaultrepos.json");
            String content = IOUtil.toString(inputStream);
            //有字符串了开始解析
            Gson gson = new Gson();
            localRepos = gson.fromJson(content,new TypeToken<List<LocalRepo>>(){}.getType());
            return localRepos;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
